<!--
 * @Author: 噜噜雯雯 2199987619@qq.com
 * @Date: 2025-06-18 16:21:26
 * @LastEditors: 噜噜雯雯 2199987619@qq.com
 * @LastEditTime: 2025-06-20 10:25:41
 * @FilePath: \linePatrol\vue-element-plus-admin\src\views\Emergency\components\Detail.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<script setup lang="tsx">
import { PropType, ref } from 'vue'
import { Descriptions, DescriptionsSchema } from '@/components/Descriptions'
import { Form } from '@/components/Form'
import { Dialog } from '@/components/Dialog'
import { useForm } from '@/hooks/web/useForm'
import { EmergencyEventType } from '@/api/Emergency/type'
import EmergencyApi from '@/api/Emergency'
import { ElMessage } from 'element-plus'
import { FormHooks, DetailHooksSchema } from './Detail'
import { useEmergencyStore } from '@/store/modules/emergency'
const { formRegister, formMethods } = useForm()
const Store = useEmergencyStore()
const { setValues, getFormData, getElFormExpose } = formMethods
const { trackFile, FormSchemas, rules, saveLoading, dialogVisible, TrackFormData } = FormHooks()
const { DistributeDetailSchema, SolveDetailSchema, TrackDetailSchema } = DetailHooksSchema()
const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<EmergencyEventType>>,
    default: () => null
  },
  detailSchema: {
    type: Array as PropType<DescriptionsSchema[]>,
    default: () => []
  }
})
Store.setEventAssign(props.currentRow?.uuid) //指派详情
Store.setEventSolve(props.currentRow?.uuid) //处置详情
Store.setEventTrack(props.currentRow?.uuid) //跟踪详情
//新增跟踪
const AddTrack = () => {
  dialogVisible.value = true
  trackFile.value = []
  TrackFormData.value = { eventUuid: props.currentRow?.uuid }
  setValues(TrackFormData.value)
}
//提交新增跟踪表单信息
const save = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    formData.trackFile = JSON.stringify(trackFile.value)
    const res: any = ref(false)
    res.value = await EmergencyApi.AddPlanEventTrack(formData)
      .catch(() => {})
      .finally(() => {
        saveLoading.value = false
      })
    if (res.value) {
      dialogVisible.value = false
      ElMessage.success('追踪新增成功')
      Store.setEventTrack(props.currentRow?.uuid) //跟踪详情
    }
  }
}
</script>

<template>
  <div class="relative">
    <Descriptions
      :title="'隐患详情' + '工单编号' + '---' + props.currentRow?.serialNo"
      :schema="detailSchema"
      :data="currentRow || {}"
    />
    <Descriptions
      v-for="item in Store.getEventAssign"
      :key="item.uuid"
      :title="'指派记录---' + item.createTime"
      :schema="DistributeDetailSchema"
      :data="item || {}"
    />
    <Descriptions
      v-for="item in Store.getEventSolve"
      :key="item.uuid"
      :title="'处置记录---' + item.createTime"
      :schema="SolveDetailSchema"
      :data="item || {}"
    />
    <Descriptions
      v-for="item in Store.getEventTrack"
      :key="item.eventUuid"
      :title="'跟踪记录---' + item.createTime"
      :schema="TrackDetailSchema"
      :data="item || {}"
    />
    <BaseButton
      type="primary"
      style="position: absolute; top: 0; z-index: 99; right: 50px"
      @click="AddTrack"
    >
      新增跟踪
    </BaseButton>
    <Dialog style="width: 500px" v-model="dialogVisible" title="新增跟踪">
      <Form :rules="rules" @register="formRegister" :schema="FormSchemas" />
      <template #footer>
        <BaseButton type="primary" :loading="saveLoading" @click="save"> 提交 </BaseButton>
        <BaseButton @click="dialogVisible = false">关闭</BaseButton>
      </template>
    </Dialog>
  </div>
</template>
<style scoped>
:deep(.el-form-item__label) {
  /* font-size: 18px; */
  font-weight: 600;
  margin: 5px 0;
}
</style>
